﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Data.SqlClient;
using DreamBuilder.Core.DAO;
using DreamBuilder.UI.UserControls.DataAccess.Services;
using DreamBuilder.Core.Container;


namespace DreamBuilder.UI.UserControls.DataAccess.Components
{
    public class MsSqlRefListDAL : RefListDAL
    {
        public override string RefListDBHelperString { get; set; }
        public override DataTable SelectListPaged(string select, string from, string where, string orderBy, string ascOrDesc, int intPageIndex, int intPageSize, out int recordCount)
        {
            DbParameter p_recordCount = new SqlParameter("RecordCount", SqlDbType.Int);
            p_recordCount.Direction = ParameterDirection.Output;
            DbParameter[] parameters = new DbParameter[]
            {
                DB.CreateSqlParameter("SelectStatement",select, ParameterDirection.Input,DB.ParameterType.Varchar),
                DB.CreateSqlParameter("FromStatement",from, ParameterDirection.Input,DB.ParameterType.Varchar),
                DB.CreateSqlParameter("WhereStatement",where, ParameterDirection.Input,DB.ParameterType.Varchar),
                DB.CreateSqlParameter("OrderByExpression",orderBy, ParameterDirection.Input,DB.ParameterType.Varchar),
                DB.CreateSqlParameter("AscOrDesc",ascOrDesc, ParameterDirection.Input,DB.ParameterType.Varchar),
                DB.CreateSqlParameter("PageIndex",intPageIndex, ParameterDirection.Input,DB.ParameterType.Int),
                DB.CreateSqlParameter("PageSize",intPageSize, ParameterDirection.Input,DB.ParameterType.Int),
                p_recordCount
            };
            if (!string.IsNullOrEmpty(RefListDBHelperString))
            {
                IDBHelper dbHelper = DBHelperFactory.CreateInstance(RefListDBHelperString);
                DataTable dtInfo = dbHelper.RunProcedureDataTable("spSelectListDynamicPaged2", parameters);
                recordCount = Convert.ToInt32(p_recordCount.Value);
                return dtInfo;
            }
            else
            {
                recordCount = 0;
                return null;
            }
        }
    }
}
